const app = getApp()

const { lawApi } = require('../../../api/services/law/index') //法律库
const { dangerApi, spThinkJhkApi, spThinkSclApi } = require('../../../api/services/danger/index') //隐患库

Page({
  data: {
    params: { pageNum: 1, pageSize: 10 },
    safeOption: [
      { text: '法律库', value: 0 },
      { text: '隐患库', value: 1 },
      { text: '风控库', value: 2 },
    ],
    safeValue: 0,
    dataMore: true,
    listData: [],
    state: 0,
    isLoading: false, //节流状态值
    active: 0,
    spThinkJhk: [], //作业危害分析库列表
    spThinkScl: [], //安全检查分析库列表
  },
  // 搜索
  onSearch(event) {
    let keyword = event.detail
    switch (this.data.state) {
      case 0:
        this.setData({ listData: [], dataMore: true, params: { pageNum: 1, pageSize: 10, fileName: keyword } })
        this.getLawList()
        break
      case 1:
        this.setData({ listData: [], dataMore: true, params: { pageNum: 1, pageSize: 10, state: keyword } })
        this.getDangerList()
        break
    }
  },
  // 智库选择
  changeOption(event) {
    let name = event.detail
    this.setData({ state: name })
    switch (name) {
      case 0:
        this.setData({ listData: [], dataMore: true })

        this.getLawList()
        break

      case 1:
        this.setData({ listData: [], dataMore: true })
        this.getDangerList()
        break

      case 2:
        // 默认作业危害分析库列表
        this.setData({ listData: [], dataMore: false })
        this.getSpThinkJhk()
        break
    }
  },

  // 获取法律库
  async getLawList() {
    this.setData({ isLoading: true })
    if (!this.data.dataMore) return

    const res = await lawApi(this.data.params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    console.log(res)
    const newList = [...this.data.listData, ...res.rows]
    this.setData({
      listData: newList,
      isLoading: false,
      dataMore: newList.length < res.total,
    })
  },

  // 隐患库
  async getDangerList() {
    if (!this.data.dataMore) return

    const res = await dangerApi(this.data.params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    console.log(res)
    const newList = [...this.data.listData, ...res.rows]
    this.setData({
      listData: newList,
      dataMore: newList.length < res.total,
    })
  },

  // 选择tabs
  onChangeTabs(event) {
    console.log(event.detail.name)
    switch (event.detail.name) {
      case 0:
        this.getSpThinkJhk()
        break
      case 1:
        this.getSpThinkScl()
        break
    }
  },

  // 作业危害分析库列表
  async getSpThinkJhk() {
    const res = await spThinkJhkApi(this.data.params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    console.log(res)
    const newList = [...this.data.spThinkJhk, ...res.rows]
    this.setData({
      spThinkJhk: newList,
      dataMore: newList.length < res.total,
    })
  },

  // 安全检查分析库列表
  async getSpThinkScl() {
    const res = await spThinkSclApi(this.data.params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    console.log(res)
    const newList = [...this.data.spThinkScl, ...res.rows]
    this.setData({
      spThinkScl: newList,
      dataMore: newList.length < res.total,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLawList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoading) return
    switch (this.data.state) {
      case 0:
        this.data.params.pageNum++
        this.getLawList()
        break

      case 1:
        this.data.params.pageNum++
        this.getDangerList()
        break

      case 2:
        break
    }
  },
})
